home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / lib / picture / picture.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-01  |  5.6 KB  |  305 lines

  1. #include "../Fly3D.h"
  2.  
  3. FLY_API textcache *tc=0;
  4. FLY_API int textfilter=1;
  5. FLY_API int mipmap=1;
  6.  
  7. picture::picture() 
  8.     buf=0; 
  9.     name[0]=0; 
  10.     sx=sy=bytespixel=size=0; 
  11. }
  12.  
  13. picture::~picture() 
  14.     FreePicture(); 
  15. };
  16.  
  17. int picture::CreatePicture32(int xd,int yd)
  18. {
  19.     int a;
  20.     sx=xd; sy=yd; bytespixel=4;
  21.  
  22.     size=sx*sy*bytespixel;
  23.  
  24.     if ((buf=new unsigned char *[sy])!=0)
  25.        {
  26.         buf[0]=new unsigned char[sx*sy*bytespixel];
  27.         if (!buf[0])
  28.             {
  29.             delete buf;
  30.             buf=0;
  31.             return 0;
  32.             }
  33.         for( a=1;a<sy;a++ )
  34.             buf[a]=&buf[0][a*sx*bytespixel];
  35.         
  36.         return 1;
  37.        }
  38.     return 0;
  39. }
  40.  
  41. int picture::CreatePicture24(int xd,int yd)
  42. {
  43.     int a;
  44.     sx=xd; sy=yd; bytespixel=3;
  45.  
  46.     size=sx*sy*bytespixel;
  47.  
  48.     if ((buf=new unsigned char *[sy])!=0)
  49.        {
  50.         buf[0]=new unsigned char[sx*sy*bytespixel];
  51.         if (!buf[0])
  52.             {
  53.             delete buf;
  54.             buf=0;
  55.             return 0;
  56.             }
  57.         for( a=1;a<sy;a++ )
  58.             buf[a]=&buf[0][a*sx*bytespixel];
  59.         
  60.         return 1;
  61.        }
  62.     return 0;
  63. }
  64.  
  65. void picture::FreePicture(void)
  66. {
  67.     if (buf)
  68.         {
  69.         delete buf[0];
  70.         delete buf;
  71.         }
  72.     buf=0;
  73. }
  74.  
  75. int picture::LoadPIC(char *file)
  76. {
  77.     char *c=strrchr(file,'.');
  78.     if (c==0)
  79.         return 0;
  80.     c++;
  81.     
  82.     if (!stricmp(c,"tga"))
  83.         return LoadTGA(file);
  84.     else
  85.     if (!stricmp(c,"jpg"))
  86.         return LoadJPG(file);
  87.     
  88.     return 0;
  89. }
  90.  
  91. int picture::LoadJPG(char *file)
  92. {
  93.     FreePicture();
  94.     JPEGDATA data;
  95.     fly_pak fp;
  96.     FILE *f;
  97.  
  98.     f=fp.get_fp(file);
  99.     if (f==0) return 0;
  100.     memset(&data,0,sizeof(JPEGDATA));
  101.     data.input_file=f;
  102.     JpegInfo(&data);
  103.     fclose(f);
  104.     if (data.components==3)
  105.         {
  106.         CreatePicture24(data.width,data.height);
  107.  
  108.         f=fp.get_fp(file);
  109.  
  110.         data.ptr             = buf[0];
  111.         data.input_file      = f;
  112.         data.hWnd            = 0;
  113.         data.ProgressMsg     = 0;
  114.  
  115.         JpegRead(&data);
  116.         fclose(f);
  117.  
  118.         if (data.status==0)
  119.             return 1;
  120.         }
  121.     return 0;
  122. }
  123.  
  124. int picture::LoadTGA(char *file)
  125. {
  126.     fly_pak fp;
  127.     int x_pos1, x_pos2, y, i, p, cursize;
  128.     unsigned char flag, TGA_INI[18], pixel_order;
  129.  
  130.     FreePicture();
  131.  
  132.     sx=sy=0;
  133.  
  134.     if(!fp.open(file))
  135.         return 0;
  136.  
  137.     fp.read((char *)&TGA_INI[0], 18);
  138.         
  139.     if(!((TGA_INI[16]==24 || TGA_INI[16]==32) && (TGA_INI[2]==2 || TGA_INI[2]==10)))
  140.         return 0;
  141.  
  142.     sx=*((unsigned short *)&TGA_INI[12]);
  143.     sy=*((unsigned short *)&TGA_INI[14]);
  144.  
  145.     if(TGA_INI[16]==24)
  146.         CreatePicture24(sx,sy);
  147.     else
  148.         CreatePicture32(sx,sy);
  149.  
  150.     pixel_order=TGA_INI[17]&0x30;
  151.  
  152.     if(TGA_INI[2]==2)
  153.         fp.read(buf[0], size);
  154.     else
  155.     {
  156.         unsigned char *p=buf[0], *q, c;
  157.  
  158.         cursize=0;
  159.         while(cursize<sx*sy)
  160.         {
  161.             fp.read(&c, 1);
  162.             if(!(c&0x80))
  163.             {
  164.                 c++;
  165.                 fp.read(p, c*bytespixel);
  166.                 p+=c*bytespixel;
  167.             }
  168.             else
  169.             {
  170.                 c=(c&0x7f)+1;
  171.                 fp.read(p, bytespixel);
  172.                 q=p;
  173.                 for(i=1; i<c; i++)
  174.                 {
  175.                     q+=bytespixel;
  176.                     q[0]=p[0];
  177.                     q[1]=p[1];
  178.                     q[2]=p[2];
  179.                 }
  180.                 p=q;
  181.                 p+=bytespixel;
  182.             }
  183.             cursize+=c;
  184.         }
  185.     }
  186.  
  187.     if (pixel_order==0x00 || pixel_order==0x10)
  188.         {
  189.         unsigned char *line=new unsigned char[bytespixel*sx];
  190.         for( i=0;i<sy/2;i++ )
  191.             {
  192.             memcpy(line,buf[i],bytespixel*sx);
  193.             memcpy(buf[i],buf[sy-i-1],bytespixel*sx);
  194.             memcpy(buf[sy-i-1],line,bytespixel*sx);
  195.             }
  196.         delete line;
  197.         }
  198.  
  199.     for( y=0;y<sy;y++ )
  200.     {
  201.         for(p=0; p<sx; p++)
  202.         {
  203.             x_pos1=p*bytespixel;
  204.             flag=buf[y][x_pos1+2];
  205.             buf[y][x_pos1+2]=buf[y][x_pos1];
  206.             buf[y][x_pos1]=flag;
  207.         }
  208.         if(pixel_order==0x10 || pixel_order==0x30)
  209.             for(p=0; p<sx/2; p++)
  210.             {
  211.                 x_pos1=p*bytespixel;
  212.                 x_pos2=(sx-1-p)*bytespixel;
  213.                 for(i=0; i<bytespixel; i++)
  214.                 {
  215.                     flag=buf[y][x_pos2+i];
  216.                     buf[y][x_pos2+i]=buf[y][x_pos1+i];
  217.                     buf[y][x_pos1+i]=flag;
  218.                 }
  219.             }
  220.     }
  221.  
  222.     return 1;
  223. }
  224.  
  225. int picture::SaveTGA(char *file)
  226. {
  227.     FILE *fp;
  228.     int a,b;
  229.     unsigned char TGA_INI[18];
  230.     unsigned char *picline;
  231.  
  232.     picline=new unsigned char[sx*bytespixel];
  233.     if (!picline)
  234.        return 0;
  235.  
  236.     if ((fp=fopen(file,"wb"))!=0)
  237.        {
  238.         memset(&TGA_INI[0],0,18);
  239.         TGA_INI[12]=(unsigned char)(sx%256);
  240.         TGA_INI[13]=(unsigned char)(sx/256);
  241.         TGA_INI[14]=(unsigned char)(sy%256);
  242.         TGA_INI[15]=(unsigned char)(sy/256);
  243.         TGA_INI[2]=2;
  244.         TGA_INI[16]=8*bytespixel;
  245.         fwrite((char *)&TGA_INI[0],18,1,fp);
  246.         for( a=sy-1;a>=0;a-- )
  247.              {
  248.               for( b=0;b<sx;b++ )
  249.                    {
  250.                     picline[b*bytespixel]=buf[a][b*bytespixel+2];
  251.                     picline[b*bytespixel+1]=buf[a][b*bytespixel+1];
  252.                     picline[b*bytespixel+2]=buf[a][b*bytespixel];
  253.                     if (bytespixel==4)
  254.                         picline[b*bytespixel+3]=buf[a][b*bytespixel+3];
  255.                    }
  256.               if (fwrite((char *)picline,sx,bytespixel,fp)!=(unsigned)bytespixel)
  257.                  {
  258.                   fclose(fp);
  259.                   delete picline;
  260.                   return 0;
  261.                  }
  262.              }
  263.         fclose(fp);
  264.        }
  265.     delete picline;
  266.     return 1;
  267. }
  268.  
  269. int picture::CreateNormalMap(float scale)
  270. {
  271.     if (bytespixel!=4)
  272.         return 0;
  273.  
  274.     unsigned char *uc=buf[0];
  275.     float c,cx,cy,dcx,dcy,nx,ny,nz;
  276.     int wx=sx*bytespixel;
  277.     int i,j;
  278.     float one255=1.0f/255.0f,len;
  279.  
  280.     for( i=0;i<sx;i++ )
  281.         for( j=0;j<sy;j++ )
  282.         {
  283.             c = uc[3] * one255;
  284.             cx = buf[i][((j+1)%sx)*4+3] * one255;
  285.             cy = buf[(i+1)%sy][j*4+3] * one255;
  286.             dcx = scale * (c - cx);
  287.             dcy = scale * (c - cy);        
  288.  
  289.             len = dcx*dcx + dcy*dcy + 1.0f;
  290.             nz = (float)(1.0/sqrt(len));
  291.             nx = dcy*nz;
  292.             ny = -dcx*nz;
  293.  
  294.             uc[0] = (unsigned char)(128 + 127*nx);
  295.             uc[1] = (unsigned char)(128 + 127*ny);
  296.             uc[2] = (unsigned char)(128 + 127*nz);
  297.             uc+=4;
  298.         }
  299.  
  300. //    SaveTGA("teste.tga");
  301.  
  302.     return 1;
  303. }